from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError


def password_validator(comment):
    if len(comment) < 6:
        raise ValidationError(u"密码不少于6位")


def username_validator(username):
    user = User.objects.filter(username=username)
    if user:
        raise ValidationError('邮箱已被占用')

class LoginForm(forms.Form):
    # 用户邮箱
    email = forms.CharField(required=True,
    error_messages={
            "required": u'邮箱不能为空'
        },
        )

    # 用户密码
    password = forms.CharField(
        error_messages={
            "required": u'密码不能为空'
        },
        validators=[password_validator]
    )


class RegisterForm(forms.Form):
    # 用户名
    name = forms.CharField(required=True)

    # 用户邮箱
    email = forms.EmailField(
        error_messages={
            "required": u'邮箱不能为空'
        },
        validators=[username_validator]
    )

    # 用户密码
    password = forms.CharField(
        error_messages={
            "required": u'密码不能为空'
        },
        validators=[password_validator]
    )
